﻿using DDLParser.constant;
using DDLParser.model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDLParser.sqlserver
{
    public class SqlServerIterator : DDLIterator
    {
        public SqlServerIterator(StreamReader stream)
            : base(stream)
        {
            OneCharTerminator.Add(new DDLTerm() { Term = SqlServerTerminator.OPEN_PARENTHESIS, Type = SqlServerTerminatorType.OPEN_PARENTHESIS });
            OneCharTerminator.Add(new DDLTerm() { Term = SqlServerTerminator.CLOSE_PARENTHESIS, Type = SqlServerTerminatorType.CLOSE_PARENTHESIS });
            OneCharTerminator.Add(new DDLTerm() { Term = SqlServerTerminator.COMMA, Type = SqlServerTerminatorType.COMMA });
            OneCharTerminator.Add(new DDLTerm() { Term = SqlServerTerminator.HYPHEN, Type = SqlServerTerminatorType.HYPHEN });
            OneCharTerminator.Add(new DDLTerm() { Term = SqlServerTerminator.SLASH, Type = SqlServerTerminatorType.SLASH });
            OneCharTerminator.Add(new DDLTerm() { Term = SqlServerTerminator.ASTERISK, Type = SqlServerTerminatorType.ASTERISK });
            OneCharTerminator.Sort();
        }

        protected override bool DoTerminatorTerm(int terminatorType)
        {
            bool isBreak = false;
            if (terminatorType == SqlServerTerminatorType.OPEN_PARENTHESIS ||
                terminatorType == SqlServerTerminatorType.CLOSE_PARENTHESIS)
            {
                isBreak = DoParenthesis(terminatorType);
            }
            else if (terminatorType == SqlServerTerminatorType.COMMA)
            {
                isBreak = DoComma(terminatorType);
            }
            else if (terminatorType == SqlServerTerminatorType.HYPHEN||
                     terminatorType == SqlServerTerminatorType.SLASH||
                     terminatorType == SqlServerTerminatorType.ASTERISK)
            {
                isBreak = DoComment(terminatorType);
            }
            return isBreak;
        }

        protected bool DoParenthesis(int terminatorType)
        {
            bool isBreak = true;

            if (WordLength <= 0)
            {
                TextOffset++;
                WordLength = 1;
            }

            return isBreak;
        }

        protected bool DoComma(int terminatorType)
        {
            bool isBreak = true;

            if (WordLength <= 0)
            {
                TextOffset++;
                WordLength = 1;
            }

            return isBreak;
        }

        protected bool DoComment(int terminatorType)
        {
            bool isBreak = true;

            if (WordLength <= 0)
            {
                TextOffset++;
                WordLength = 1;
            }

            return isBreak;
        }
    }
}
